$(function () {
    $("#jqGrid").Grid({
        url: '../devaccount/list',
        colModel: [
            {label: 'id', name: 'id', index: 'ID', align: 'center', key: true, hidden: true},

            {label: '设备名称', name: 'devName', index: 'DEV_NAME', width: 80, align: 'center'},
            {label: '设备编码', name: 'devCode', index: 'DEV_CODE', width: 80, align: 'center'},
            {label: '所属车间', name: 'devWorkshop', index: 'DEV_WORKSHOP', width: 80, align: 'center'},
            {label: '所属工厂', name: 'devFactories', index: 'DEV_FACTORIES', width: 80, align: 'center'},
            {label: '生产线名称', name: 'devProducName', index: 'DEV_PRODUC_NAME', width: 80, align: 'center'},
            {
                label: '特种设备',
                name: 'isSpec',
                index: 'IS_SPEC',
                width: 80,
                align: 'center',
                formatter: function (value) {
                    return transIsSpec(value);
                }
            },
            {label: '工序号', name: 'devProceNo', index: 'DEV_PROCE_NO', width: 80, align: 'center'},
            {
                label: '状态',
                name: 'devStatus',
                index: 'DEV_STATUS',
                width: 80,
                align: 'center',
                formatter: function (value) {
                    return transDevStatus(value);
                }
            },
            {
                label: '操作时间',
                name: 'createTime',
                index: 'CREATE_TIME',
                width: 80,
                align: 'center',
                formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd');
                }
            },
            {label: '备注', name: 'remark', index: 'REMARK', width: 80, align: 'center'},
            /*
            {label: '删除标识', name: 'delFlag', index: 'DEL_FLAG', width: 80,align: 'center'},
            {label: '修改时间', name: 'updateTime', index: 'UPDATE_TIME', width: 80,align: 'center'},
            {label: '修改人', name: 'updateUser', index: 'UPDATE_USER', width: 80,align: 'center'},
            {label: '操作人', name: 'createUser', index: 'CREATE_USER', width: 80,align: 'center'},*/
        ]
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        devAccount: {
            updateTime: '',
            updateUser: '',
            createUser: '',
            createTime: '',
            delFlag: '',
            remark: '',
            devStatus: '',
            isSpec: '',
            devProceNo: '',
            devProducName: '',
            devWorkshop: '',
            devFactories: '',
            devName: '',
            devCode: '',
            id: '',
        },
        ruleValidate: {
            devName: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: ''
        },
        statusList: enumsToList('STATUS'),
        isSpecList: enumsToList('SPEC_TYPE'),
        deviceinfoList: {}
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.devAccount = {
                updateTime: '',
                updateUser: '',
                createUser: '',
                createTime: '',
                delFlag: '',
                remark: '',
                devStatus: 1,
                isSpec: '',
                devProceNo: '',
                devProducName: '',
                devWorkshop: '',
                devFactories: '',
                devName: '',
                devCode: '',
                id: '',
            };
            vm.getDevInfo()
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.devAccount = {
                updateTime: '',
                updateUser: '',
                createUser: '',
                createTime: '',
                delFlag: '',
                remark: '',
                devStatus: '',
                isSpec: '',
                devProceNo: '',
                devProducName: '',
                devWorkshop: '',
                devFactories: '',
                devName: '',
                devCode: '',
                id: '',
            };
            vm.getInfo(id);
            vm.getDevInfo()
        },
        saveOrUpdate: function (event) {
            var url = vm.devAccount.id == (null || '') ? "../devaccount/save" : "../devaccount/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.devAccount),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRow("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../devaccount/delete/" + ids,
                    //  params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {

            Ajax.request({
                url: "../devaccount/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.devAccount = r.devAccount;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        getDevInfo: function () {
            Ajax.request({
                url: "../deviceinfo/queryAll",
                async: true,
                successCallback: function (r) {
                    vm.deviceinfoList = r.list;
                }
            });
        },
        getDevDetail: function (devCode) {
            Ajax.request({
                url: "../deviceinfo/getInfoByDevCode/" + devCode,
                type: 'POST',
                successCallback: function (r) {
                    if (r.deviceInfo) {
                        vm.devAccount.devName = r.deviceInfo.devName
                        vm.devAccount.devWorkshop = r.deviceInfo.devFarm    //车间
                        vm.devAccount.devCode = r.deviceInfo.devCode   //devCode
                        // vm.devAccount.devStatus = r.deviceInfo.devStatus   //工序号 devProceNo
                        vm.devAccount.devProceNo = r.deviceInfo.devProceNo   //工序号 devProceNo
                        vm.devAccount.devProducName = r.deviceInfo.devProceName  //工序名称 devProceName
                        vm.devAccount.isSpec = parseInt(r.deviceInfo.devKind)//设备类型
                    }

                }
            });
        }
    }
});